Public Class Form1 Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " ... deleted ... #End Region Const UPPER As Integer = 9 Const LOWER As Integer = 1 Const gasPrice As Double = 2.0 'Const gasToTravel As Double = 1.0 Const MPG As Double = 20.0 Dim inpf As IO.StreamReader Dim distf As IO.StreamReader Dim distances(30, 30) As Integer ' Dim junk(30, 30, 50, 40, 50, 30) As Integer Dim cities(30) As String Dim numLoc As Integer = 0 Dim currLocIndex As Integer Dim randGen As New Random Private Function ExtractLine(ByVal text As String, ByRef line As String, ByRef startPos As Integer, ByRef foundPos As Integer) As Boolean ' find the next occurrence of the new line char foundPos = text.IndexOf(ControlChars.NewLine, startPos) ' check if it was found If foundPos = -1 Then Return False Else ' pull out the current line of the file line = text.Substring(startPos, foundPos - startPos) ' update the starting point for search startPos = foundPos + 2 Return True End If End Function Private Function randPts() As Integer Dim rand As Integer rand = randGen.Next(LOWER, UPPER + 1) 'rand = Convert.ToInt32((UPPER - LOWER + 1) * Rnd() + LOWER) Return rand End Function Private Function calcGasToTravel(ByVal dist(,) As Integer, ByVal startInd As Integer, ByVal endInd As Integer) As Double Dim gas As Double Dim distance As Integer distance = dist(startInd, endInd) gas = distance / MPG Return gas End Function Private Sub BtnQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnQuit.Click Me.Close() End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim rand As Integer Dim row As Integer Dim col As Integer Dim allText As String Dim lineExists As Boolean Dim currentCity As String Dim start As Integer = 0 Dim foundPos As Integer ' seed random number generator based on time Randomize() ' read cities in along with distances between them 'If IO.File.Exists("cities.txt") Then 'inpf = IO.File.OpenText("cities.txt") 'If IO.File.Exists("morecities.txt") Then If My.Computer.FileSystem.FileExists("cities.txt") Then ' inpf = IO.File.OpenText("morecities.txt") ' read the file contents allText = My.Computer.FileSystem.ReadAllText("cities.txt") ' while not at end, extract line Do lineExists = ExtractLine(allText, currentCity, start, foundPos) If lineExists Then cities(numLoc) = currentCity numLoc = numLoc + 1 End If Loop While lineExists And numLoc <= 30 ' now read distances from other file 'If IO.File.Exists("distances.txt") Then 'distf = IO.File.OpenText("distances.txt") ' If IO.File.Exists("moredistances.txt") Then start = 0 foundPos = 0 Dim rowStr As String Dim distancesStr As String If My.Computer.FileSystem.FileExists("distances.txt") Then ' distf = IO.File.OpenText("moredistances.txt") distancesStr = My.Computer.FileSystem.ReadAllText("distances.txt") Do lineExists = ExtractLine(distancesStr, rowStr, start, foundPos) 'Do While distf.Peek <> -1 And row < numLoc 'Dim rowStr As String ' rowStr = distf.ReadLine() col = 0 Dim remStr As String remStr = rowStr Do While col < numLoc - 1 Dim commapos As Integer commapos = remStr.IndexOf(",") Dim currStr As String currStr = remStr.Substring(0, commapos) remStr = remStr.Substring(commapos + 1) Dim val As Integer Dim isConverted As Boolean isConverted = Integer.TryParse(currStr, val) If Not isConverted Then MsgBox("data problem ... exiting") Me.Close() End If ' val = Convert.ToInt32(currStr) distances(row, col) = val col = col + 1 Loop ' get last distance in row distances(row, col) = Convert.ToInt32(remStr) row = row + 1 Loop While lineExists And row < numLoc Else MsgBox("distance file not found, program cannot run") End If Else MsgBox("city file not found, program cannot run") End If ' put us in the first city TxtLocation.Text = cities(0) currLocIndex = 0 ' fill other point values Dim cnt As Integer LstPts.Items.Add(cities(0) & ", 0") For cnt = 1 To numLoc - 1 Dim line As String rand = randPts() line = cities(cnt) & ", " & rand LstPts.Items.Add(line) Next End Sub Private Sub BtnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnGo.Click Dim newPts As Integer Dim pts As Integer Dim gasAvail As Double Dim gasToTravel As Double Dim endLoc As Integer Dim rand As Integer endLoc = LstPts.SelectedIndex gasAvail = Convert.ToDouble(TxtGas.Text) If endLoc <> -1 Then gasToTravel = calcGasToTravel(distances, currLocIndex, endLoc) If gasAvail >= gasToTravel Then pts = Convert.ToInt32(TxtPoints.Text) ' determine points for going there Dim selectLine As String selectLine = LstPts.SelectedItem Dim commapos As Integer commapos = selectLine.IndexOf(",") Dim ptsStr As String ptsStr = selectLine.Substring(commapos + 1) newPts = Convert.ToInt32(ptsStr) ' move us to the new location TxtLocation.Text = cities(endLoc) currLocIndex = endLoc ' generate new points values LstPts.Items.Clear() Dim cnt As Integer For cnt = 0 To numLoc - 1 If cnt = endLoc Then ' going to same city isn't worth any points LstPts.Items.Add(cities(cnt) & ", 0") Else ' generate random points for going there Dim line As String rand = randPts() line = cities(cnt) & ", " & rand LstPts.Items.Add(line) End If Next pts = pts + newPts TxtPoints.Text = Convert.ToString(pts) gasAvail = gasAvail - gasToTravel TxtGas.Text = gasAvail.ToString("n1") Else MsgBox("not enough gas to travel") End If End If End Sub Private Sub btnFuel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFuel.Click Dim moneyAvail As Double Dim gasAvail As Double moneyAvail = Convert.ToDouble(TxtCash.Text) gasAvail = Convert.ToDouble(TxtGas.Text) If moneyAvail >= gasPrice Then moneyAvail = moneyAvail - gasPrice gasAvail = gasAvail + 1 TxtCash.Text = moneyAvail.ToString("n2") TxtGas.Text = gasAvail.ToString("n1") Else MsgBox("not enough money to buy gas") End If End Sub End Class